package com.ytjj.qmyx.mall.api.h5.v1.seckill;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.constants.SysConfigConstants;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.SysConfigUtil;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.enums.ActivityTypeEnums;
import com.ytjj.qmyx.mall.model.response.NewKillResponse;
import com.ytjj.qmyx.mall.service.ActivityService;
import com.ytjj.qmyx.mall.service.CommonService;
import com.ytjj.qmyx.users.client.UsersClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * 活动控制器
 *
 * 
 * @version 1.0
 * 
 */
@RestController
@RequestMapping("/v1/seckill")
@Slf4j
public class SeckillController extends BaseController {

    private final ActivityService activityService;

    private final CommonService commonService;

    private final UsersClient usersClient;

    @Resource
    private RedisService redisService;

    @Autowired
    private SysConfigUtil sysConfigUtil;

    public SeckillController(ActivityService activityService, CommonService commonService, UsersClient usersClient) {
        this.activityService = activityService;
        this.commonService = commonService;
        this.usersClient = usersClient;
    }

    /**
     * 获取9.9秒杀的今日热抢和明日预告
     */
    @GetMapping("/getNineSecKillColumn")
    public CommonResult getNineSecKillColumn() {
        NewKillResponse newKillResponse;
        String key = com.ytjj.qmyx.mall.constants.RedisConstants.ACTIVITY_PRODUCT + ActivityTypeEnums.NEW_KILL_V2.name() + TokenUtil.getDataType() + "_NinePointNine_" + TokenUtil.getPlatformData();
        String s = redisService.get(key);
        if (StringUtils.isNotEmpty(s)) {
            newKillResponse = JSONObject.parseObject(s, NewKillResponse.class);
        } else {
            newKillResponse = activityService.getNineSecKillColumn();
            redisService.set(key, JSONObject.toJSONString(newKillResponse));
            redisService.expire(key, Integer.valueOf(sysConfigUtil.getSysConfig(SysConfigConstants.REDIS_TIME)));
        }
        return CommonResult.success(newKillResponse);
    }

}
